<?php
declare (strict_types=1);

namespace app\listener;

use app\admin\controller\ActivityOption;
use app\common\model\ActivityJoinUser;
use app\event\ActivityOpen;
use app\wechat\controller\Order;
use think\Exception;
use think\facade\Db;

class ActivityOpenListener
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle(ActivityOpen $event)
    {
        $id = $event->GetId();
        try {
            //改变活动状态变成2,已结束
            Db::name('activity')
                ->where('id', $id)
                ->update(['status' => 2, 'update_time' => time(), 'end_time' => time()]);
            //找到对应活动的join_user的数据,然后抽出对应人数的中奖人员,状态改成2,是中奖,其他改成1
            $join_user = Db::name('activity_join_user')
                ->alias('ju')
                ->Join('user u', 'ju.user_id=u.id')
                ->where('ju.activity_id', $id)
                ->column('ju.id,ju.user_id,u.openid,ju.activity_id');

            //先去改变用户参与表的状态
            Db::name('activity_join_user')
                ->whereIn('id', array_column($join_user, 'id'))
                ->update(['status' => 1]);
            //中奖人员生成一个特殊唯一中奖编码
            $win_user = array_rand($join_user, $event->GetWinNum());
            $win_user = is_array($win_user) ? $win_user : [$win_user];
//            dd($win_user);
            foreach ($join_user as $k=>$v){
                if (in_array($k,$win_user)){
                    $prize_code=get_no($event->GetId(), $v['id'], $event->GetCode());
                    Db::name('activity_join_user')
                        ->where('id', $v['id'])
                        ->update([
                            'status' => 2,
                            'prize_code' => $prize_code,
                        ]);
                    open_message($v['openid'], $v['activity_id'], $event->GetName(),$prize_code);
                }else{
                    open_message_regret($v['openid'], $v['activity_id'], $event->GetName(),'0');
                }
            }
//            foreach ($win_user as $key) {
//                $prize_code=get_no($event->GetId(), $join_user[$key]['id'], $event->GetCode());
//                Db::name('activity_join_user')
//                    ->where('id', $join_user[$key]['id'])
//                    ->update([
//                        'status' => 2,
//                        'prize_code' => $prize_code,
//                    ]);
//                open_message($join_user[$key]['openid'], $join_user[$key]['activity_id'], $event->GetName(),$prize_code);
//            }
            //给中奖人员发送通知
            //判断是否为置顶活动,如果为置顶活动,自动发布置顶活动
//            if ($event->GetType() == 1) {
//                $option = Db::name('activity_option')
//                    ->where('id', 1)
//                    ->find();
//                $result['name'] = $option['name'];
//                $result['img'] = $option['img'];
//                $result['max_num'] = $option['max_num'];
//                $result['integral_num'] = $option['integral_num'];
//                $result['win_num'] = $option['win_num'];
//                $result['status'] = 1;
//                $result['create_time'] = time();
//                $result['update_time'] = time();
//                $result['type'] = $option['type'];
//                $result['code'] = $option['code'];
//                $res = Db::name('activity')
//                    ->insertGetId($result);
//                SetRedis('max_num' . $res, $option['max_num'], 4);
//                SetRedis('join_num' . $res, 0, 4);
//            }
        } catch (\Exception $e) {
            //如果开奖失败,提醒
            Db::name('log')
                ->insert(['title' => '编号:' . $event->GetId() . '开奖失败,请查看',
                    'content' => $e->getMessage(),
                    'create_time' => time(),
                    'update_time' => time()
                ]);
            //给冰帝发送短信,第一时间处理

        }
    }
}
